package com.yh.diary_dao;

import java.text.DateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.afa.tourism.greendao.gen.DaoMaster;
import com.afa.tourism.greendao.gen.DaoMaster.DevOpenHelper;
import com.afa.tourism.greendao.gen.DaoSession;
import com.afa.tourism.greendao.gen.DiaryDao;

/**
 * Created by yxf on 2018/6/17.
 */

public class DiaryEditor extends AppCompatActivity {
    private static final String TAG = "Diary";
    public static final int MENU_ITEM_DELETE = Menu.FIRST + 1;
    public static final String EDIT_DIARY_ACTION = "DiaryEditor.EDIT_DIARY";
    public static final String INSERT_DIARY_ACTION = "DiaryEditor.action.INSERT_DIARY";
    private EditText mTitleText;
    private EditText mBodyText;
    private Button confirmButton;
    private long mid;
    private int mState;
    private static final int STATE_INSERT = 1;
    private Button backBtn;
    // GreenDAO使用的变量

    private SQLiteDatabase db;

    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private DiaryDao diaryDao;
    private Diary diary;
    private Cursor cursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setTheme(android.R.style.Theme_Black);
        final Intent intent = getIntent();
        final String action = intent.getAction();
        setContentView(R.layout.diary_edit);
        mTitleText = (EditText) findViewById(R.id.title);
        mBodyText = (EditText) findViewById(R.id.body);
        confirmButton = (Button) findViewById(R.id.confirm);
        backBtn=(Button) findViewById(R.id.back);

        initDAO();

        if (EDIT_DIARY_ACTION.equals(action)) {// 编辑日记
            Bundle bundle = new Bundle();
            bundle = this.getIntent().getExtras();
            mid = bundle.getLong("id");
            diary = diaryDao.loadByRowId(mid);
            mTitleText.setTextKeepState(diary.getTitle());
            mBodyText.setTextKeepState(diary.getBody());

        } else if (INSERT_DIARY_ACTION.equals(action)) {// 新建日记
            mState = STATE_INSERT;
        } else {
            finish();
            return;
        }

        confirmButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                if (mState == STATE_INSERT) {
                    insertDiary();
                } else {
                    updateDiary();
                }
                Intent intent = new Intent(DiaryEditor.this, MainActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.left_in,R.anim.left_out);
                DiaryEditor.this.finish();
                overridePendingTransition(R.anim.left_in,R.anim.left_out);
            }
        });
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mState == STATE_INSERT) {
                    String title = mTitleText.getText().toString();
                    String body = mBodyText.getText().toString();
                    if(!title.isEmpty()||!body.isEmpty()){
                        insertDiary();
                    }
                } else {
                    updateDiary();
                }
                Intent intent = new Intent(DiaryEditor.this, MainActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.left_in,R.anim.left_out);
                DiaryEditor.this.finish();
                overridePendingTransition(R.anim.left_in,R.anim.left_out);
            }
        });
        mTitleText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(event.getKeyCode()==KeyEvent.KEYCODE_ENTER){
                    Toast.makeText(DiaryEditor.this, "不可以换行哦", Toast.LENGTH_SHORT).show();
                    return true;
                }else{
                    return false;
                }
            }
        });
        String title = mTitleText.getText().toString();
        if(title.equals("untitled")) mTitleText.setText("");
    }

    private void initDAO() {
        DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db",
                null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        diaryDao = daoSession.getDiaryDao();
    }

    private void insertDiary() {
        String title = mTitleText.getText().toString();
        String body = mBodyText.getText().toString();
        final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                DateFormat.MEDIUM);
        String date = df.format(new Date());
        if(title.isEmpty())title="untitled";
        Diary diary = new Diary(null, title, body, date);
        diaryDao.insert(diary);
    }

    private void updateDiary() {
        String title = mTitleText.getText().toString();
        String body = mBodyText.getText().toString();
        final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                DateFormat.MEDIUM);
        String date = df.format(new Date());
        if(title.isEmpty())title="untitled";
        diary.setTitle(title);
        diary.setBody(body);
        diary.setDate(date);
        diaryDao.update(diary);
    }

    //识别退出返回上一层而不是退出软件

    public void onBackPressed() {
        if (mState == STATE_INSERT) {
            String title = mTitleText.getText().toString();
            String body = mBodyText.getText().toString();
            if(!title.isEmpty()||!body.isEmpty()){
                insertDiary();
            }
        } else {
            updateDiary();
        }
        Intent intent = new Intent(DiaryEditor.this, MainActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.left_in,R.anim.left_out);
        DiaryEditor.this.finish();
        overridePendingTransition(R.anim.left_in,R.anim.left_out);
    }

}
